void merge_sort(int array[], int length)  
{  
    if(NULL == array || 0 == length)  
        return ;  
    _merge_sort(array, 0, length-1);  
}  
void _merge_sort(int array[], int start, int end)
{
    if(start >= end)
        return;

    int middle = start + ((end - start) >> 1);
    _merge_sort(array, start, middle);
    _merge_sort(array, middle + 1, end);
    _merge_data_in_array(array, start, middle, end);
}
void _merge_data_in_array(int array[], int start, int middle, int end)
{
    int length = end - start + 1;
    int* pData = NULL;
    int left = start;
    int right = middle + 1;
    int all = 0;

    /* allocate new memory to the space */
    pData = (int*) malloc(sizeof(int) * length);
    assert(NULL != pData);
    memset(pData, 0, length);

    /* begin to move data */
    while(right <= end){
        while(array[left] <= array[right] && left <= middle){
            pData[all] = array[left]; left ++; all ++;
        }

        if(left > middle)  {
            break;
        }

        while(array[left] > array[right] && right <= end){
            pData[all] = array[right]; right ++; all ++;
        }
    }

    /* move the left data */
    if(left <= middle)
        memmove(&pData[all], &array[left], sizeof(int) * (middle -left +1));

    if(right <= end)
        memmove(&pData[all], &array[right], sizeof(int) * (end - right + 1));
    
    memmove(&array[start], pData, sizeof(int) * length);
    free(pData);
}